import pygame
from random import randint


class Enemy(pygame.sprite.Sprite):
    def __init__(self,width,height):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load('images/enemy1.png').convert_alpha()
        self.destroy_list=[
                pygame.image.load('images/enemy1_down1.png').convert_alpha(),
                pygame.image.load('images/enemy1_down2.png').convert_alpha(),
                pygame.image.load('images/enemy1_down3.png').convert_alpha(),
                pygame.image.load('images/enemy1_down4.png').convert_alpha()
                ]
        self.sound=pygame.mixer.Sound('sound/enemy1_down.wav')
        self.rect=self.image.get_rect()
        self.rect.x=randint(0,width-self.rect.width)
        self.rect.y=randint(-5*height,0)
        self.active=True
        self.speed=4
        self.mask=pygame.mask.from_surface(self.image)
        self.destroy_index=0
        self.width,self.height=(width,height)
        self.flag=1
        self.score=0
    def move(self):
        self.rect.y+=self.speed
        if self.rect.y>self.height:
            self.reset()

    def draw_active(self,surface):
        surface.blit(self.image,self.rect)

    def draw_destroy(self,surface):
        if self.destroy_index <12:
            surface.blit(self.destroy_list[self.destroy_index//3],self.rect)                
            self.destroy_index+=1
        else :
            self.reset()
            self.score=1000

        
    def draw(self,surface):
        if self.active:
            self.draw_active(surface)
        else :
            self.draw_destroy(surface)
    def reset(self):
        self.destroy_index=0
        self.rect.x=randint(0,self.width-self.rect.width)
        self.rect.y=randint(-5*self.height,0)
        self.active=True
        self.score=0



